<div class="layui-card">
	<div style="margin-bottom: 15px;padding-top:15px;height: 40px;">
        <button style="float: right;margin-right: 20px;" class="layui-btn layuiadmin-btn-tags" data-type="add">添加</button>	        	
    </div>  
  <script type="text/html" id="LAY-good-list-table-edit1">
  	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
  </script>
   <table id="LAY-good-comment" lay-filter="LAY-good-comment"></table>
</div>
<script>
    //Demo
  layui.use(['admin','form', 'table'], function(){
    var form = layui.form;
    var $ = layui.$;
    var admin = layui.admin;
    var view = layui.view;
    var table = layui.table;
    var cols = [[
             {field:'keyword', title: '热搜词',align: 'center',}
            ,{width:300, align:'center', fixed: 'right',title: '操作', toolbar: '#LAY-good-list-table-edit1'}
          ]];
	  table.set({
	    headers: { //通过 request 头传递
	      'access-token': layui.data('layuiAdmin').access_token
	    }
	  });
	  // 删除评论
	  table.on('tool(LAY-good-comment)', function(obj){
	  	var data = obj.data;
	  	if(obj.event === 'del'){
        layer.confirm('是否删除', function(index){
   	    $.ajax({
			    type: "DELETE",
			    dataType: "json",
			    url: "https://bookshop.dingdangtoy.com/api/admin/hot_keyword/destroy",
			    data: {id:data.id},
			    headers: { //通过 request 头传递
	              'access-token': layui.data('layuiAdmin').access_token
	            },
				success: function(data){
				    layer.msg('操作成功');
				    layui.table.reload('LAY-good-comment'); //重载表格
		        layer.close(index); //执行关闭 
				},
				error: function(msg){
				  console.log(msg)
				}
			});
        });	  		
	  	} else if(obj.event === 'edit'){
	      admin.popup({
	        title: '编辑热搜词'
	        ,area: ['400px', '250px']
	        ,id: 'LAY-popup-content-edit'
	        ,success: function(layero, index){
	          view(this.id).render('goods/keyword/add', data).done(function(){
	            form.render(null, 'layuiadmin-app-form-list');
	            
	            //监听提交
	            form.on('submit(layuiadmin-app-form-submit)', function(data){
	              var field = data.field; //获取提交的字段
						$.ajax({
							type: "PUT",
							dataType: "json",
							url: "https://bookshop.dingdangtoy.com/api/admin/hot_keyword/update",
							data: {id: data.field.id,keyword:data.field.keyword},
							headers: { //通过 request 头传递
								'access-token': layui.data('layuiAdmin').access_token
							},
							success: function(data) {
								layer.msg('操作成功');
								layui.table.reload('LAY-good-comment'); //重载表格
							},
							error: function(msg) {
								console.log(msg)
							}
						});	
	              //提交 Ajax 成功后，关闭当前弹层并重载表格
	              //$.ajax({});	             
	              layer.close(index); //执行关闭 
	            });
	          });
	        }
	      });
	    }
	  })  
	  var active = {
		    add: function(){
		      admin.popup({
		        title: '添加热搜词'
		        ,area: ['400px', '250px']
		        ,id: 'LAY-popup-content-tags'
		        ,success: function(layero, index){
		          view(this.id).render('goods/keyword/add').done(function(){
		            form.render(null, 'layuiadmin-app-form-list');
		         
		            //监听提交
		            form.on('submit(layuiadmin-app-form-submit)', function(data){
		              var title = data.field.title; //获取提交的字段
		              var sort = data.field.sort;
		              //提交 Ajax 成功后，关闭当前弹层并重载表格
					        layer.confirm('是否执行此操作', function(index){
					   	        	$.ajax({
										type: "POST",
										dataType: "json",
										url: "https://bookshop.dingdangtoy.com/api/admin/hot_keyword/store",
										data: {keyword:data.field.keyword},
										headers: { //通过 request 头传递
											'access-token': layui.data('layuiAdmin').access_token
										},
										success: function(data) {
											layer.msg('操作成功');
											layui.table.reload('LAY-good-comment'); //重载表格
										},
										error: function(msg) {
											console.log(msg)
										}
									});	
					        });			         
									 layer.close(index); //执行关闭 
		            });
		          });
		        }
		      });
		    }   
		  };

	    table.render({
	      elem: '#LAY-good-comment'
	      ,url: 'https://bookshop.dingdangtoy.com/api/admin/hot_keyword/index'
	      ,parseData: function(res) { //res 即为原始返回的数据
				return {
					"code": res.code, //解析接口状态
					"msg": res.message, //解析提示文本
					"count": res.data._meta.total_count, //解析数据长度
					"data": res.data.items //解析数据列表
				}
		  }
	      ,cols: cols
	      ,page: true
	    });            
	   $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
	    var type = $(this).data('type');
	    active[type] ? active[type].call(this) : '';
	  });  
  });
</script>
<style>

</style>